package com.mall.b2c.domain.member;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlTransient;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mall.common.domain.AreaDomain;
import com.mall.common.domain.BaseEntity;

/**
 * 实体类 - 收货地址
 */
@Entity
@Table(name="b2c_receiver")
public class ReceiverDomain extends BaseEntity {

	private static final long serialVersionUID = 6667934590013089481L;
	private String alias;  //地址别名，标签（例如：“送到家里”、“送到公司”）
	private String name;// 收货人姓名
	@JsonIgnore private AreaDomain area;// 收货地区路径
	private String address;// 地址
	private String contact;// 联系方式
	private String zipCode;// 邮编
	private Boolean isDefault;// 是否默认
	@JsonIgnore private MemberDomain member;// 会员

	public ReceiverDomain() {}
	
	@Transient
	public JSONObject getJson(){
		JSONObject obj = new JSONObject();
		obj.put("id", this.getId());
		obj.put("alias", this.getAlias());
		obj.put("name", this.getName());
		obj.put("address", address);
		obj.put("zipCode", zipCode);
		obj.put("fallArea", area.getFull_name());
		obj.put("contact", contact);
		return obj;
	}
	
	@Transient
	public Long getProvinceId(){
		if(area != null){
			String areaPath = area.getAreaPath();
			if(StringUtils.isEmpty(areaPath)){
				return area.getId();
			}else{
				return Long.parseLong(areaPath.split(",")[0]);
			}
		}
		return null;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	@Column(nullable = false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(nullable = false ,name="area_id")
	public AreaDomain getArea() {
		return area;
	}

	public void setArea(AreaDomain area) {
		this.area = area;
	}

	@Column(nullable = false, length = 5000)
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	@Column(nullable = false)
	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	@Column(columnDefinition="tinyint(1) default 0")
	public Boolean getIsDefault() {
		return isDefault;
	}

	public void setIsDefault(Boolean isDefault) {
		this.isDefault = isDefault;
	}

	
	public void setMember(MemberDomain member) {
		this.member = member;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(nullable = false ,name="member_id")
	@XmlTransient
	public MemberDomain getMember() {
		return member;
	}

}